package com.ferryc.modules.sys.web;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ferryc.commons.entity.TreeEntity;
import com.ferryc.commons.web.BaseController;
import com.ferryc.constant.GlobalConstant;
import com.ferryc.modules.sys.entity.SysMenu;
import com.ferryc.modules.sys.service.ISysMenuService;

@Controller
public class IndexController extends BaseController {

	/** */
	@Autowired
	private ISysMenuService sysMenuService;

	/**
	 * 首页
	 *
	 * @param model
	 * @return
	 */
	@GetMapping("/index")
	public String index(Model model) {

		List<SysMenu> menuList = sysMenuService.getUserMenuById(getUserId());

		model.addAttribute("resList", createMenu(menuList, SysMenu.SYSTEM_ID));
		return "index";
	}

	private List<TreeEntity<SysMenu>> createMenu(List<SysMenu> resList, String pid) {
		List<TreeEntity<SysMenu>> childResList = new ArrayList<>();
		for (Iterator iterator = resList.iterator(); iterator.hasNext();) {
			SysMenu sysMenu = (SysMenu) iterator.next();
			if (sysMenu.getParentId().equals(pid)) {
				TreeEntity<SysMenu> te = new TreeEntity<SysMenu>();
				te.setParent(sysMenu);
				if (GlobalConstant.YES.equals(sysMenu.getSelected())) {
					te.setChild(createMenu(resList, sysMenu.getId()));
				}
				childResList.add(te);
			}
		}
		return childResList;
	}

	@RequestMapping(value = "/main", method = RequestMethod.GET)
	public String main(Model model) {
		return "main";
	}

}
